package com.mjh.manager;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import org.apache.log4j.Logger;
import com.mjh.bean.Article;
import com.mjh.bean.ArticleDetail;
import com.mjh.comm.page.Pager;
import com.mjh.config.GlobalConfig;
import com.mjh.dao.ArticleDAO;
import com.mjh.dao.ArticleDetailDAO;
import com.mjh.dao.ArticleSearchDAO;
import com.mjh.comm.Constant;

public class ArticleSearchManager {

	public static Logger log = Logger.getLogger(ArticleSearchManager.class);
	
	private ArticleSearchDAO searchDao = new ArticleSearchDAO();
	private ArticleDAO articleDao = new ArticleDAO();
	private ArticleDetailDAO articleDetailDao = new ArticleDetailDAO();
	
	private List<Integer> appendArticleDetails(List<Article> articles){
		List<Integer> articleIds = new ArrayList<Integer>(articles.size());
		for(Article article : articles){
			articleIds.add(article.getId());
		}
		List<ArticleDetail> details = articleDetailDao.selectByArticleIds(articleIds);
		Pager<ArticleDetail> page = null;
		List<ArticleDetail> detailPage = null;
		for(Article article : articles){
			page = new Pager<ArticleDetail>();
			detailPage = new ArrayList<ArticleDetail>();
			for(ArticleDetail detail : details){
				if(article.getId() == detail.getArticleId()){
					detailPage.add(detail);
				}
			}
			page.setPageData(detailPage);
			article.setDetailsPage(page);
		}
		return articleIds;
	}
	
	public Pager<Article> search(int pagerNo, String keyword){
		Pager<Article> pager = new Pager<Article>();
		pager.setPageNo(pagerNo);
		pager.setPageSize(GlobalConfig.getSysConfig().getArticleListPageSize());
		List<Integer> ids;
		try {
			ids = searchDao.searchIndex(keyword, pager);
			if(ids.size() > 0){
				pager.setPageData(articleDao.selectByIds(ids));
			}
		} catch (Exception e) {
			log.error(e);
		}
		return pager;
	}
	
	public static void main(String[] args) {
		ArticleSearchManager mgr = new ArticleSearchManager();
		System.out.println(mgr.search(1, "春节").getTotalNum());
	}
	
	public int createIndex(){
		HashMap<String,Object> parameter = new HashMap<String,Object>();
		List<Article> articles = articleDao.selectForList(parameter);
		if(articles.size() == 0) return Constant.SUCCESS;
		List<Integer> articleIds = appendArticleDetails(articles);
		try {
			searchDao.addIndex(articles, true);
			parameter.put("indexStatus", Constant.ARTICLE_IS_INDEX);
			parameter.put("ids", articleIds);
			articleDao.updateIndexStatus(parameter);
		} catch (Exception e) {
			log.error(e);
			return Constant.FAIL;
		}
		return Constant.SUCCESS;
	}
	
	public int addIndex(){
		HashMap<String,Object> parameter = new HashMap<String,Object>();
		parameter.put("isIndex", Constant.ARTICLE_NOT_INDEX);
		List<Article> articles = articleDao.selectForList(parameter);
		if(articles.size() == 0) return Constant.SUCCESS;
		List<Integer> articleIds = appendArticleDetails(articles);
		try {
			searchDao.addIndex(articles, false);
			parameter.put("indexStatus", Constant.ARTICLE_IS_INDEX);
			parameter.put("ids", articleIds);
			articleDao.updateIndexStatus(parameter);
		} catch (Exception e) {
			log.error(e);
			return Constant.FAIL;
		}
		return Constant.SUCCESS;
	}
	
	public int updateIndex(){
		HashMap<String,Object> parameter = new HashMap<String,Object>();
		parameter.put("isIndex", Constant.ARTICLE_INDEX_OLD);
		List<Article> articles = articleDao.selectForList(parameter);
		if(articles.size() == 0) return Constant.SUCCESS;
		List<Integer> articleIds = appendArticleDetails(articles);
		try {
			searchDao.updateIndex(articles);
			parameter.put("indexStatus", Constant.ARTICLE_IS_INDEX);
			parameter.put("ids", articleIds);
			articleDao.updateIndexStatus(parameter);
		} catch (Exception e) {
			log.error(e);
			return Constant.FAIL;
		}
		return Constant.SUCCESS;
	}
	
	public int delIndex(int articleId){
		try {
			searchDao.deleteIndex(articleId);
		} catch (Exception e) {
			log.error(e);
			return Constant.FAIL;
		}
		return Constant.SUCCESS;
	}
	
	public boolean isIndexExist(){
		try {
			return searchDao.isIndexExist();
		} catch (Exception e) {
			log.error(e);
			return false;
		}
	}
}
